package com.itheima.publisher;

/**
 * Created with IntelliJ IDEA 2023.
 * Description:
 * User: homechen
 * Date: 2024-04-21
 * Time: 14:33
 */

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 生产者的确认消息机制
 */
@Slf4j
@Component
public class config implements ApplicationContextAware {
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        // 设置消息回调
        RabbitTemplate rabbitTemplate = applicationContext.getBean(RabbitTemplate.class);
        rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {
            @Override
            public void returnedMessage(ReturnedMessage returned) {
                log.info("消息发送失败：交换机{ }  路由键{ }  消息{ }  状态码{ } 原因{ } ",
                        returned.getExchange(), returned.getRoutingKey(), returned.getReplyText(),
                        returned.getReplyCode(), returned.getReplyText());
            }
        });
    }
}
